﻿using stdCall.File;
using stdCall.Models;
using stdCall;
using System;
using System.Windows.Forms;

namespace stdCall.Resource
{
    /// <summary>
    /// 资源加载类，包含名单和点名的文本文件路径（静态）
    /// </summary>
    class ResourseLoader
    {
        private StudentsManager studentManager = new StudentsManager();
        private UnluckyCallsManager unluckyCallsManager = new UnluckyCallsManager();
        private static  String fileSTDSPath = @".\students.txt";
        private static String fileCourseIDPath = @".\course.txt";
   /*     public static Hashtable static_controls = new Hashtable();*/


        public static String FileCourseIDPath
        {
            get { return ResourseLoader.fileCourseIDPath; }
            set { ResourseLoader.fileCourseIDPath = value; }
        }

        public static String FileSTDSPath
        {
            get { return ResourseLoader.fileSTDSPath; }
        }

        private static  String fileUCSPath = @".\unluckycalls.txt";

        public static String FileUCSPath
        {
            get { return ResourseLoader.fileUCSPath; }
        } 

        private FileHelper fileHelper = new FileHelper();
        public static int CourseID = 0;


        /// <summary>
        /// 加载所有系统资源（学生列表，点名历史，课程ID）
        /// </summary>
        public void loadSystemResourse()
        {
            loadUnluckyCalls();
            loadStudents();
        }

        /// <summary>
        /// 加载学生表
        /// </summary>
        public void loadStudents()
        {
            fileHelper.openReadFile(fileSTDSPath);
            while (!FileHelper.ReadFile.EndOfStream)
            {
                String stdNum = fileHelper.readSingleData();
                String stdName = fileHelper.readSingleData();
                new StudentsManager().addStudent(stdNum,stdName,new UnluckyCallsManager().getCallNumByStdNum(stdNum));
            }
            fileHelper.closeFile();
        }

        /// <summary>
        /// 加载点名历史表
        /// </summary>
        public void loadUnluckyCalls()
        {
            fileHelper.openReadFile(fileUCSPath);
            while (!FileHelper.ReadFile.EndOfStream)
            {
                String stdNum = fileHelper.readSingleData();
                String star = fileHelper.readSingleData();
                String courseID = fileHelper.readSingleData();

                new UnluckyCallsManager().addUnluckyCall(stdNum, int.Parse(star), int.Parse(courseID));
            }
            fileHelper.closeFile();
        }

        /// <summary>
        /// 加载课程ID，未实现
        /// </summary>
        public void loadCourseID()
        {
          /*  CourseID = 2;*/
            fileHelper.openReadFile(fileCourseIDPath);
            CourseID = int.Parse(fileHelper.readSingleData().Trim())+1;
            fileHelper.closeFile();
        }


        /// <summary>
        /// 设置此堂课程编号
        /// </summary>
        /// <param name="courseid"></param>
        public void setCourseID(int courseid)
        {
            CourseID = courseid;
            FileHelper fh = new FileHelper();
            fh.closeFile();
            fh.openWriteFile(ResourseLoader.FileCourseIDPath,false);
            try
            {
                fh.writeSingleData(courseid.ToString());
            }
            catch (System.Exception ex)
            {
                ThreadExceptionDialog warnDialog = new ThreadExceptionDialog(ex);
                warnDialog.ShowDialog();
            }
            fh.closeFile();
        }


    }
}
